Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
remark-mdx
Advanced tools
The remark-mdx npm package is a plugin for remark, a markdown processor powered by plugins part of the unified collective. It enables MDX syntax in markdown documents processed by remark. MDX is an authorable format that lets you seamlessly write JSX in your markdown documents. This package allows you to parse and serialize MDX files, making it possible to use MDX with the remark ecosystem.
Parsing MDX syntax
This feature allows you to parse strings of MDX and transform them into a markdown abstract syntax tree (AST).
const remark = require('remark');
const remarkMdx = require('remark-mdx');
remark().use(remarkMdx).processSync('<h1>Hello, MDX!</h1>').toString();
Serializing MDX
This feature enables you to serialize the markdown AST back into a string of MDX, which can then be saved to a file or processed further.
const remark = require('remark');
const remarkMdx = require('remark-mdx');
const vfile = require('to-vfile');
const file = vfile.readSync('example.mdx');
const result = remark().use(remarkMdx).processSync(file).toString();
console.log(result);
Remark is the core library that powers the remark ecosystem, which includes remark-mdx. It is a markdown processor that can be extended with plugins but does not support MDX syntax out of the box.
Rehype is another processor in the unified collective that works with HTML instead of markdown. It can be used in conjunction with remark-mdx to transform the markdown AST into a rehype (HTML) AST and manipulate it further.
remark plugin to support the MDX syntax (JSX, expressions, import/exports).
This package is a remark plugin to support the MDX syntax.
This plugin is useful if you’re dealing with the MDX syntax and integrating with remark, rehype, and the rest of unified. Some example use cases are when you want to lint the syntax or compile it to something other that JavaScript.
remark is an AST (abstract syntax tree) based transform project.
The layer under remark is called mdast, which is just the syntax tree without
the convention on how to transform.
mdast is useful when transforming to other formats.
Another layer underneath is micromark, which is just the parser and has support
for concrete tokens.
micromark is useful for linting and formatting.
remark-mdx
is a small wrapper to integrate all of these.
Its parts can be used separately.
Typically though, you’d want to move a layer up: @mdx-js/mdx
.
That package is the core compiler for turning MDX into JavaScript which
gives you the most control.
Or even higher: if you’re using a bundler (webpack, Rollup, esbuild), or a site
builder (Gatsby, Next.js) or build system (Vite, WMR) which comes with a
bundler, you’re better off using an integration: see
§ Integrations.
This package is ESM only:
Node 12+ is needed to use it and it must be import
ed instead of require
d.
npm:
npm install remark-mdx
yarn:
yarn add remark-mdx
import {remark} from 'remark'
import remarkMdx from 'remark-mdx'
const file = remark()
.use(remarkMdx)
.processSync('import a from "b"\n\na <b /> c {1 + 1} d')
console.log(String(file))
Yields:
import a from "b"
a <b/> c {1 + 1} d
This package exports no identifiers.
The default export is remarkMdx
.
unified().use(remarkMdx)
Configures remark so that it can parse and serialize MDX (JSX, expressions, import/exports). It doesn’t do anything with the syntax: you can create your own plugin to transform them.
This plugin applies several micromark extensions to parse the syntax. See their readmes for parse details:
micromark-extension-mdx-expression
— expressions ({1 + 1}
)micromark-extension-mdx-jsx
— JSX (<div />
)micromark-extension-mdxjs-esm
— ESM (export x from 'y'
)micromark-extension-mdx-md
— Turn off HTML, autolinks, and indented codeThis plugin applies several mdast utilities to build and serialize the AST. See their readmes for the node types supported in the tree:
mdast-util-mdx-expression
— expressions ({1 + 1}
)mdast-util-mdx-jsx
— JSX (<div />
)mdast-util-mdxjs-esm
— ESM (export x from 'y'
)This package is fully typed with TypeScript.
If you’re working with the syntax tree, make sure to import this plugin somewhere in your types, as that registers the new node types in the tree.
/**
* @typedef {import('remark-mdx')}
*/
import {visit} from 'unist-util-visit'
export default function myRemarkPlugin() => {
/** @param {import('@types/mdast').Root} tree */
return (tree) => {
visit(tree, (node) => {
// `node` can now be one of the nodes for JSX, expressions, or ESM.
})
}
}
Alternatively, in TypeScript, do:
/// <reference types="remark-mdx" />
import type {Root} from '@types/mdast'
import {visit} from 'unist-util-visit'
export default function myRemarkPlugin() => {
return (tree: Root) => {
visit(tree, (node) => {
// `node` can now be one of the nodes for JSX, expressions, or ESM.
})
}
}
See § Security on our website for information.
See § Contribute on our website for ways to get started. See § Support for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
FAQs
remark plugin to support MDX syntax
We found that remark-mdx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.